package driver

import (
	"github.com/gin-gonic/gin"
	v1 "golang-sgg-daijia/api/v1"
	"golang-sgg-daijia/internal/handler"
	"golang-sgg-daijia/internal/service"
	"net/http"
)

type FileHandler struct {
	*handler.Handler
	CosService service.CosService
	FileService service.FileService
}

func NewFileHandler(
	handler *handler.Handler,
	fileService service.FileService,
) *FileHandler {
	return &FileHandler{
		Handler:    handler,
		FileService: fileService,
	}
}

func (h *FileHandler) Upload(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	url, err := h.FileService.Upload(ctx, file)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrBadRequest, nil)
		return
	}
	v1.HandleSuccess(ctx, url)
}
